package gri_user_alert_config;

# vim: ts=4:sw=4:syntax=perl
use xml_helper;
use Data::Dumper;

my $error="";

sub new {
my $self={};
shift;
my $f=shift;


	#################################################
	# If a file is not specified then we create 	#
	# empty object that can be populated.			#
	#################################################
	$self->{xml_helper}=new xml_helper();

	if(!defined($f)) {
		$self->{user}=undef;
		$self->{email}=undef;
		$self->{reports}={};
		$self->{filename}=undef;
		bless($self);
		return $self;
	}

	my $d=""; my $fd;
	if(!open($fd,$f)) {
		$error="Can not open file '$f' : $!";
		return(undef);
	}
	$self->{filename}=$f;
	$d=join("",<$fd>);
	close($fd);
	my $x=$self->{xml_helper}->parse($d);
	if(! $x) {
		$error="Unable to parse XML!";
		return undef;
	}

	# Check the data is in the format we expect.

	my ($data,$attrs,$elements)=$self->{xml_helper}->get_element("");
	if(!defined($data)) {
		$error="Invalid data to parse [top level]";
		return(undef);
	}
	if(scalar(@$elements)!=1) {
		$error="More than 1 top level element!";
		return(undef);
	}
	if($elements->[0] ne "user_alert") {
		$error="Top level element is not 'user_alert'";
	}
	($data,$attrs,$elements)=$self->{xml_helper}->get_element("user_alert#0");
	if(!defined($data)) {
		$error="Invalid data to parse [user_alert#0]";
		return(undef);
	}
	#################################################################
	# We expect the user and email elements to be here - abort	#
	# if not.							#
	#################################################################
	if(! grep {$_ eq "user_alert#0|user#0"} @$elements) {
		$error="Missing 'user' element.";
		return(undef);
	}
	if(! grep {$_ eq "user_alert#0|email#0"} @$elements) {
		$error="Missing 'email' element.";
		return(undef);
	}
	for my $cent (@$elements) {
		if(! grep {$_ eq $cent} ("user_alert#0|user#0","user_alert#0|email#0")) {
			if($cent !~ /^user_alert#0\|report#\d+/) {
				$error="Unexpected element '$cent' found.";
				return(undef);
			}
		}
	}
	#################################################################
	# Get the data and store in the object for easy reference.	#
	#################################################################
	my ($data2,$attrs2,$elements2)=$self->{xml_helper}->get_element("user_alert#0|user#0");
	$self->{user}=$data2;
	($data2,$attrs2,$elements2)=$self->{xml_helper}->get_element("user_alert#0|email#0");
	$self->{email}=$data2;
	#################################################################
	# We now get details of all the reports...			#
	#################################################################
	my $rep=0;
	$self->{reports}={};
	while(1) {
		($data2,$attrs2,$elements2)=$self->{xml_helper}->get_element("user_alert#0|report#$rep");
		if(defined($data2)) {
			if(exists($attrs2->{portal}) && exists($attrs2->{report})) {
				$self->{reports}->{$attrs2->{report}}=$attrs2->{portal};
			}
			$rep++;
		} else {
			last;
		}
	}
	bless($self);
	return $self;
}

sub get_email {
my $self=shift;
	return $self->{email};
}

sub get_report_count {
my $self=shift;

	return scalar(keys(%{$self->{reports}}));
}

sub get_report_names {
my $self=shift;

	return keys(%{$self->{reports}});
}

sub get_named_report {
my $self=shift;

	return $self->{reports}->{$_[0]};
}

sub error {
	return $error;
}

sub set_username($$) {
my $self=shift;

	$self->{user}=$_[0];
}

sub set_email($$) {
my $self=shift;

	$self->{email}=$_[0];
}

sub set_filename($$) {
my $self=shift;

	$self->{filename}=$_[0];
}

#########################################################################
# The set reports is sent a list of report_name,portal_name pairs.	#
#########################################################################

sub set_reports {
my $self=shift;
my %reports=@_;

	$self->{reports}={};
	for my $ckey (keys %reports) {
		$self->{reports}->{$ckey}=$reports{$ckey};
	}
}

sub write_file {
my $self=shift;
my $aa=new xml_helper();

	if(!defined($self->{filename})) {
		$error="Filename not yet defined.";
		return(0);
	}
	if(!defined($self->{user})) {
		$error="Username not defined.";
		return(0);
	}
	if(!defined($self->{email})) {
		$error="Email address not defined.";
		return(0);
	}

	#################################################################
	# Has the basic details so write out the file.			#
	#################################################################

	my $n=$self->{filename};
	unlink("$n.tmp") if -f "$n.tmp";
	if(-e "$n.tmp") {
		$error="Unable expected non-file '$n.tmp' found.";
		return(0);
	}
	my $fd;
	if(!open($fd,">$n.tmp")) {
		$error="Unable to open '$n.tmp' for writing: $!";
		return(0);
	}

	if(! print $fd "<?xml version=\"1.0\" standalone=\"yes\"?>\n") {
		$error="Unable to write to file: $!";
		return(0);
	}
	if(! print $fd "<user_alert>\n\t<user>" . $aa->string_to_xml($self->{user}) . "</user>\n\t<email>" . $aa->string_to_xml($self->{email}) ."</email>\n") {
		$error="Unable to write to file: $!";
		return(0);
	}
	#################################################################
	# Now write all the lines for all the reports...		#
	#################################################################
	for my $ckey (keys %{$self->{reports}}) {
		if(! print $fd "\t<report portal=\"",$aa->string_to_xml($self->{reports}->{$ckey}),"\" report=\"",$aa->string_to_xml($ckey),"\"/>\n") {
			$error="Unable to write to file: $!";
			return(0);
		}
	}
	if(! print $fd "</user_alert>\n") {
		$error="Unable to write to file: $!";
		return(0);
	}
	close($fd);
	if(-f "$n") {
		if(!rename "$n","$n.old") {
			$error="Unable to rename old file: $!";
			return(0);
		}
	}
	if(!rename("$n.tmp",$n)) {
		$error="Unable to rename temporary file: $!";
		return(0);
	}
	return 1;
}

1;

